package com.next.androidintentlibrary;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;
import ohos.bundle.IBundleManager;
import ohos.rpc.RemoteException;
import ohos.utils.net.Uri;

import java.util.List;

public class MarketIntents {
    private Context context;
    private Intent intent;

    private MarketIntents(Context context)
    {
        this.context = context;
    }

    public static MarketIntents from(Context context)
    {
        return new MarketIntents(context);
    }

    public MarketIntents showThisAppInMarket() throws RemoteException {
        String packageName = context.getApplicationContext().getBundleName();
        return showInMarket(packageName);
    }

    public MarketIntents showThisAppInGooglePlay() throws RemoteException {
        String packageName = context.getApplicationContext().getBundleName();
        return showInGooglePlay(packageName);
    }

    public MarketIntents showThisAppInAmazon() throws RemoteException {
        String packageName = context.getApplicationContext().getBundleName();
        return showInAmazon(packageName);
    }

    // TODO:
    private MarketIntents showThisAppInBazaar()
    {
        String packageName = context.getApplicationContext().getBundleName();
        return showInBazaar(packageName);
    }

    // TODO:
    private MarketIntents showThisAppInMyket()
    {
        String packageName = context.getApplicationContext().getBundleName();
        return showInMyket(packageName);
    }

    // TODO:
    private MarketIntents showThisAppInIranApps()
    {
        String packageName = context.getApplicationContext().getBundleName();
        return showInIranApps(packageName);
    }


    // TODO:
    private MarketIntents googlePlayStoreOurProduct()
    {
        String urlApp = "market://search?q=pub:Google+Inc.";
        String urlWeb = "http://play.google.com/store/search?q=pub:Google+Inc.";
        intent = null;
        // TODO:handle it in 2 methods
        try
        {
            Intent i = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setUri(Uri.parse(urlApp));
            setFlags(i);
            startAbility(i);
        } catch (Exception e)
        {
            Intent i = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setUri(Uri.parse(urlWeb));
            setFlags(i);
            startAbility(i);
        }
        return this;
    }

    // part of googlePlayStoreOurProduct()
    private void setFlags(Intent i)
    {
        i.addFlags(1073741824);// FLAG_ACTIVITY_NO_HISTORY = 1073741824
        i.addFlags(524288);//FLAG_ACTIVITY_NEW_DOCUMENT = 524288;

    }

    public MarketIntents showInMarket(String packageName) throws RemoteException {
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(Uri.parse("market://details?id=" + packageName));
        if (!isIntentAvailable(intent))
        {
            intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setUri(Uri.parse("amzn://apps/android?p=" + packageName));
        }

        if (!isIntentAvailable(intent))
        {
            intent = null;
        }

        if (intent != null)
        {
            intent.addFlags( 1073741824| 524288);
            //Intent.FLAG_ACTIVITY_NO_HISTORY  = 1073741824
            //Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET = 524288
        }

        return this;
    }

    public MarketIntents showInGooglePlay(String packageName) throws RemoteException {
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(Uri.parse("market://details?id=" + packageName));
        intent.setBundle("com.android.vending");
        if (!isIntentAvailable(intent))
        {
            return showInWebBrowser("https://play.google.com/store/apps/details?id=" + packageName);
        }

        if (intent != null)
        {
            intent.addFlags( 1073741824| 524288);
            //Intent.FLAG_ACTIVITY_NO_HISTORY  = 1073741824
            //Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET = 524288
        }

        return this;
    }

    public MarketIntents showInAmazon(String packageName) throws RemoteException {
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(Uri.parse("amzn://apps/android?p=" + packageName));
        intent.setBundle("com.android.vending");
        if (!isIntentAvailable(intent))
        {
            return showInWebBrowser("http://www.amazon.com/gp/mas/dl/android?p=" + packageName);
        }

        if (intent != null)
        {
            intent.addFlags( 1073741824| 524288);
            //Intent.FLAG_ACTIVITY_NO_HISTORY  = 1073741824
            //Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET = 524288
        }

        return this;
    }

    // TODO:
    private MarketIntents showInBazaar(String packageName)
    {
        return this;
    }

    // TODO:
    private MarketIntents showInMyket(String packageName)
    {
        return this;
    }

    // TODO:
    private MarketIntents showInIranApps(String packageName)
    {
        return this;
    }

    public MarketIntents showInWebBrowser(String url)
    {
        if (!url.startsWith("https://") && !url.startsWith("http://"))
        {
            url = "http://" + url;
        }
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(Uri.parse(url));
        return this;
    }

    public MarketIntents showGooglePlay()
    {
        intent = new Intent();
        intent.setAction("android.intent.action.MAIN");
        intent.addEntity("android.intent.category.APP_MARKET");
        intent.setFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        return this;
    }

    public MarketIntents searchAppInGooglePlay(String appName)
    {
        intent = new Intent();
        intent.setAction("com.google.android.gms.actions.SEARCH_ACTION");
        intent.setParam("query", appName);
        return this;
    }

    private boolean isIntentAvailable(Intent intent) throws RemoteException {
        IBundleManager bundleManager = context.getBundleManager();
        List<AbilityInfo> list = bundleManager.queryAbilityByIntent(intent, 65536,0);
        return list.size() > 0;
    }




    public Intent build()
    {
        return intent;
    }

    private void startAbility(Intent intent)
    {
        if (!(context instanceof Ability))
        {
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        }
        context.startAbility(intent,0);
    }

    public void show()
    {
        startAbility(build());
    }
}
